package v1

import (
	"fmt"
	"gin-blog/models"
	"gin-blog/pkg/e"
	"gin-blog/pkg/setting"
	"gin-blog/pkg/util"
	"github.com/Unknwon/com"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func GetTags(c *gin.Context) {
	// c.Query可用于获取?name=test&state=1这类URL参数，而c.DefaultQuery则支持设置一个默认值
	name := c.Query("name")
	maps := make(map[string]interface{})
	data := make(map[string]interface{})

	if name != "" {
		maps["name"] = name
	}

	var state int = -1
	if arg := c.Query("state"); arg != "" {
		state = com.StrTo(arg).MustInt()
		maps["state"] = state
	}

	code := e.SUCCESS

	data["lists"] = models.GetTags(util.GetPage(c), setting.PageSize, maps)
	data["total"] = models.GetTagTotal(maps)

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})

}

func HandleGetAllData(c *gin.Context) {

	type test struct {
		Test  string `json:"test"`
		Test1 string `form:"test1"`
	}

	params := new(test)
	err := c.Bind(params)
	if err != nil {
		c.JSON(500, gin.H{})
		return
	}
	fmt.Println(params)

	//body, _ := ioutil.ReadAll(c.Request.Body)
	//fmt.Println("---body/--- \r\n " + string(body))
	name := c.PostForm("name")
	state := c.PostForm("state")
	createdBy := c.PostForm("created_by")
	log.Println("---body/--- \r\n ", name, state, createdBy)

	c.JSON(200, gin.H{
		"receive": "1024",
	})

}

func AddTags(c *gin.Context) {
	tag := new(models.Tag)
	err := c.Bind(tag)
	if err != nil {
		c.JSON(500, gin.H{
			"state": "初始化失败",
		})
		return
	}
	name := tag.Name
	state := tag.State
	createdBy := tag.CreatedBy

	valid := validation.Validation{}
	valid.Required(name, "name").Message("名称不能为空")
	valid.MaxSize(name, 100, "name").Message("名称最长为100字符")
	valid.Required(createdBy, "created_by").Message("创建人不能为空")
	valid.MaxSize(createdBy, 100, "created_by").Message("创建人最长为100字符")
	valid.Range(state, 0, 1, "state").Message("状态只允许0或1")

	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		if !models.ExistTagByName(name) {
			code = e.SUCCESS
			models.AddTag(name, state, createdBy)
		} else {
			code = e.ERROR_EXIST_TAG
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})

}

func EditTags(c *gin.Context) {

	tag := new(models.Tag)
	err := c.Bind(tag)
	if err != nil {
		c.JSON(500, gin.H{
			"state": "初始化失败",
		})
		return
	}
	id := tag.ID
	name := tag.Name
	modifiedBy := tag.ModifiedBy
	valid := validation.Validation{}
	code := e.INVALID_PARAMS
	state := tag.State | 0
	valid.Range(state, 0, 1, "state").Message("状态只允许0或1")

	valid.Required(id, "id").Message("ID不能为空")
	valid.Required(modifiedBy, "modified_by").Message("修改人不能为空")
	valid.MaxSize(modifiedBy, 100, "modified_by").Message("修改人最长为100字符")
	valid.MaxSize(name, 100, "name").Message("名称最长为100字符")

	if !valid.HasErrors() {
		code = e.SUCCESS
		if models.ExistTagByID(id) {
			data := make(map[string]interface{})
			data["modified_by"] = modifiedBy
			if name != "" {
				data["name"] = name
			}
			if state != -1 {
				data["state"] = state
			}
			models.EditTag(id, data)
		} else {
			code = e.ERROR_NOT_EXIST_TAG
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}

func DeleteTags(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("ID必须大于0")

	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		code = e.SUCCESS
		if models.ExistTagByID(id) {
			models.DeleteTag(id)
		} else {
			code = e.ERROR_NOT_EXIST_TAG
		}
	} else {
		for _, err := range valid.Errors {
			log.Fatalf("DeleteTags woring: err: %v", err)
		}
	}
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}
